<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Computes shared secret for public value of remote and local DH or ECDH key</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.openssl-pkcs7-verify.html">« openssl_pkcs7_verify</a></li>
      <li style="float: right;"><a href="function.openssl-pkey-export-to-file.html">openssl_pkey_export_to_file »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.openssl.html">OpenSSL 函数</a></li>
    <li>Computes shared secret for public value of remote and local DH or ECDH key</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.openssl-pkey-derive" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pkey_derive</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.3.0, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_pkey_derive</span> &mdash; <span class="dc-title">Computes shared secret for public value of remote and local DH or ECDH key</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.openssl-pkey-derive-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pkey_derive</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.opensslasymmetrickey.html" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span>|<span class="type"><a href="class.opensslcertificate.html" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type">array</span>|<span class="type">string</span></span> <code class="parameter">$public_key</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="class.opensslasymmetrickey.html" class="type OpenSSLAsymmetricKey">OpenSSLAsymmetricKey</a></span>|<span class="type"><a href="class.opensslcertificate.html" class="type OpenSSLCertificate">OpenSSLCertificate</a></span>|<span class="type">array</span>|<span class="type">string</span></span> <code class="parameter">$private_key</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$key_length</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pkey_derive()</strong></span> takes a set of a <code class="parameter">public_key</code>
   and <code class="parameter">private_key</code> and derives a shared secret, for either DH or EC keys.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pkey-derive-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">public_key</code></dt>

    <dd>

     <p class="para">
      <code class="parameter">public_key</code> is the public key for the derivation.
      See <a href="openssl.certparams.html" class="link">Public/Private Key parameters</a> for a list of valid values.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">private_key</code></dt>

    <dd>

     <p class="para">
      <code class="parameter">private_key</code> is the private key for the derivation.
      See <a href="openssl.certparams.html" class="link">Public/Private Key parameters</a> for a list of valid values.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">key_length</code></dt>

    <dd>

     <p class="para">
      If not zero, will set the desired length of the derived secret.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pkey-derive-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The derived secret on success 或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pkey-derive-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="openssl-pkey-derive.example.basic">
    <p><strong>示例 #1 <span class="function"><strong>openssl_pkey_derive()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Load&nbsp;in&nbsp;private&nbsp;key<br /></span><span style="color: #0000BB">$priv&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_private</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----BEGIN&nbsp;PRIVATE&nbsp;KEY-----<br />MIICJgIBADCCARcGCSqGSIb3DQEDATCCAQgCggEBAJLxRCaZ933uW+AXmabHFDDy<br />upojBIRlbmQLJZfigDaSA1f9YOTsIv+WwVFTX/J1mtCyx9uBcz0Nt2kmVwxWuc2f<br />VtCEMPsmLsVXX7xRUFLpyX1Y1IYGBVXQOoOvLWYQjpZgnx47Pkh1Ok1+smffztfC<br />0DCNt4KorWrbsPcmqBejXHN79KvWFjZmXOksRiNu/Bn76RiqvofC4z8Ri3kHXQG2<br />197JGZzzFXHadGC3xbkg8UxsNbYhVMKbm0iANfafUH7/hoS9UjAVQYtvwe7YNiW/<br />HnyfVCrKwcc7sadd8Iphh+3lf5P1AhaQEAMytanrzq9RDXKBxuvpSJifRYasZYsC<br />AQIEggEEAoIBAGwAYC2E81Y1U2Aox0U7u1+vBcbht/OO87tutMvc4NTLf6NLPHsW<br />cPqBixs+3rSn4fADzAIvdLBmogjtiIZoB6qyHrllF/2xwTVGEeYaZIupQH3bMK2b<br />6eUvnpuu4Ytksiz6VpXBBRMrIsj3frM+zUtnq8vKUr+TbjV2qyKR8l3eNDwzqz30<br />dlbKh9kIhZafclHfRVfyp+fVSKPfgrRAcLUgAbsVjOjPeJ90xQ4DTMZ6vjiv6tHM<br />hkSjJIcGhRtSBzVF/cT38GyCeTmiIA/dRz2d70lWrqDQCdp9ArijgnpjNKAAulSY<br />CirnMsGZTDGmLOHg4xOZ5FEAzZI2sFNLlcw=<br />-----END&nbsp;PRIVATE&nbsp;KEY-----<br />"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Load&nbsp;in&nbsp;public&nbsp;key<br /></span><span style="color: #0000BB">$pub&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_pkey_get_public</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----BEGIN&nbsp;PUBLIC&nbsp;KEY-----<br />MIICJDCCARcGCSqGSIb3DQEDATCCAQgCggEBAJLxRCaZ933uW+AXmabHFDDyupoj<br />BIRlbmQLJZfigDaSA1f9YOTsIv+WwVFTX/J1mtCyx9uBcz0Nt2kmVwxWuc2fVtCE<br />MPsmLsVXX7xRUFLpyX1Y1IYGBVXQOoOvLWYQjpZgnx47Pkh1Ok1+smffztfC0DCN<br />t4KorWrbsPcmqBejXHN79KvWFjZmXOksRiNu/Bn76RiqvofC4z8Ri3kHXQG2197J<br />GZzzFXHadGC3xbkg8UxsNbYhVMKbm0iANfafUH7/hoS9UjAVQYtvwe7YNiW/Hnyf<br />VCrKwcc7sadd8Iphh+3lf5P1AhaQEAMytanrzq9RDXKBxuvpSJifRYasZYsCAQID<br />ggEFAAKCAQAiCSBpxvGgsTorxAWtcAlSmzAJnJxFgSPef0g7OjhESytnc8G2QYmx<br />ovMt5KVergcitztWh08hZQUdAYm4rI+zMlAFDdN8LWwBT/mGKSzRkWeprd8E7mvy<br />ucqC1YXCMqmIwPySvLQUB/Dl8kgau7BLAnIJm8VP+MVrn8g9gghD0qRCgPgtEaDV<br />vocfgnOU43rhKnIgO0cHOKtw2qybSFB8QuZrYugq4j8Bwkrzh6rdMMeyMl/ej5Aj<br />c0wamOzuBDtXt0T9+Fx3khHaowjCc7xJZRgZCxg43SbqMWJ9lUg94I7+LTX61Gyv<br />dtlkbGbtoDOnxeNnN93gwQZngGYZYciu<br />-----END&nbsp;PUBLIC&nbsp;KEY-----<br />"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Outputs&nbsp;the&nbsp;hex&nbsp;version&nbsp;of&nbsp;the&nbsp;derived&nbsp;key<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">openssl_pkey_derive</span><span style="color: #007700">(</span><span style="color: #0000BB">$pub</span><span style="color: #007700">,</span><span style="color: #0000BB">$priv</span><span style="color: #007700">));</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div></div></div></body></html>